package com.nut.spp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nut.spp.api.req.GoodsQueryReq;
import com.nut.spp.common.Result;
import com.nut.spp.entity.*;
import com.nut.spp.enums.GoodsStatusEnum;
import com.nut.spp.mapper.*;
import com.nut.spp.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsDetailMapper goodsDetailMapper;

    @Autowired
    private GoodsComposeMapper goodsComposeMapper;

    @Override
    public Result<IPage<Goods>> listPage(GoodsQueryReq req) {
        IPage<Goods> list = goodsMapper.listPage(req.newPage(), req.getType(), req.getStatus(), req.getTitle());
        return Result.ok(list);
    }

    @Override
    public Result<List<Goods>> list(Integer type) {
        List<Goods> list = goodsMapper.list(type);
        return Result.ok(list);
    }

    @Override
    public Result<Goods> getById(Integer id) {

        Goods goods = goodsMapper.selectById(id);

        GoodsDetail detail = goodsDetailMapper.selectById(id);
        detail.filled(goods);

//        List<GoodsCompose> composeList = goodsComposeMapper.selectByGoodsId(id);
//        goods.setComposeList(composeList);

        return Result.ok(goods);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result add(Goods goods) {

        goods.setStatus(GoodsStatusEnum.INIT.getValue());

        goodsMapper.insert(goods);

        GoodsDetail detail = goods.getGoodsDetail();
        detail.setId(goods.getId());
        goodsDetailMapper.insert(detail);

//        List<GoodsCompose> composeList = goods.getComposeList();
//        if (composeList != null) {
//            for (GoodsCompose goodsCompose : composeList) {
//                goodsCompose.setGoodsId(goods.getId());
//                goodsComposeMapper.insert(goodsCompose);
//            }
//        }

        return Result.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result updateById(Goods goods) {

        if (goods.needUpdate()) {
            goodsMapper.updateById(goods);
        }

        if (goods.needUpdateDetail()) {
            GoodsDetail detail = goods.getGoodsDetail();
            goodsDetailMapper.updateById(detail);
        }

//        List<GoodsCompose> composeList = goods.getComposeList();
//        if (composeList != null) {
//            for (GoodsCompose goodsCompose : composeList) {
//                Integer id = goodsCompose.getId();
//                if (id == null) {
//                    goodsCompose.setGoodsId(goods.getId());
//                    goodsComposeMapper.insert(goodsCompose);
//                } else {
//                    goodsComposeMapper.updateById(goodsCompose);
//                }
//            }
//        }

        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        for (Integer id : ids) {
            goodsMapper.deleteById(id);
        }
        return Result.ok();
    }
}